package com.lablzh.gdxy.service.impl;

import com.lablzh.gdxy.entity.Teacher;
import com.lablzh.gdxy.entity.admin.*;
import com.lablzh.gdxy.repository.TeacherRepository;
import com.lablzh.gdxy.repository.admin.*;
import com.lablzh.gdxy.service.CollegesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;


@Service
public class CollegesServiceImpl implements CollegesService {
    @Autowired
    private CollegeRepository collegeRepository;
    @Autowired
    private MajorRepository majorRepository;
    @Autowired
    private ClassesRepository classesRepository;
    @Autowired
    private DepartmentRepository departmentRepository;
    @Autowired
    private LabRepository labRepository;

    @Autowired
    private TeacherRepository teacherRepository;

    @Override
    @CacheEvict(cacheNames = "college", allEntries = true)
    public String addCollege(College college) {
        if (collegeRepository.findByName(college.getName()) != null) {
            return "该院已存在！";
        }
        LocalDateTime localDateTime = LocalDateTime.now().withNano(0);
        college.setAddTime(localDateTime);
        if (collegeRepository.save(college) != null) {
            return "保存成功！";
        } else {
            return "保存失败！";
        }
    }

    @Override
    @CacheEvict(cacheNames = "college", allEntries = true)
    public String updateCollege(College college) {
        collegeRepository.save(college);
        return "修改成功！";
    }

    @Override
    @Caching(evict = {@CacheEvict(cacheNames = "college", allEntries = true),
            @CacheEvict(cacheNames = "department", allEntries = true),
            @CacheEvict(cacheNames = "major", allEntries = true),
            @CacheEvict(cacheNames = "classes", allEntries = true)})
    public String deleteCollege(Integer id) {
        College college = collegeRepository.findById(id).orElse(null);
        assert college != null;
        List<Department> departments = departmentRepository.findAllByCollege(college.getName());
        departmentRepository.deleteAll(departments);
        List<Major> majors = majorRepository.findAllByCollege(college.getName());
        for (Major major : majors) {
            //删除该院所有班级
            List<Classes> classes = classesRepository.findAllByMajor(major.getName());
            classesRepository.deleteAll(classes);
        }
        //删除该院所有专业
        majorRepository.deleteAll(majors);
        //删除该院
        collegeRepository.deleteById(id);
        return "删除成功！";
    }

    @Override
    @CacheEvict(cacheNames = "major", allEntries = true)
    public String addMajor(Major major) {
        if (majorRepository.findByName(major.getName()) != null) {
            return "该专业已存在！";
        }
        LocalDateTime localDateTime = LocalDateTime.now().withNano(0);
        major.setAddTime(localDateTime);
        if (majorRepository.save(major) != null) {
            return "保存成功！";
        } else {
            return "保存失败！";
        }
    }

    @Override
    @CacheEvict(cacheNames = "major", allEntries = true)
    public String updateMajor(Major major) {
        majorRepository.save(major);
        return "修改成功！";
    }

    @Override
    @Caching(evict = {@CacheEvict(value = "major", allEntries = true),
            @CacheEvict(value = "classes", allEntries = true)})
    public String deleteMajor(Integer id) {
        Major major = majorRepository.findById(id).orElse(null);
        assert major != null;
        List<Classes> classes = classesRepository.findAllByMajor(major.getName());
        //删除该专业所有班级
        classesRepository.deleteAll(classes);
        //删除专业
        majorRepository.deleteById(id);
        return "删除成功！";
    }

    @Override
    @CacheEvict(cacheNames = "classes", allEntries = true)
    public String addClasses(Classes classes) {
        if (classesRepository.findByName(classes.getName()) != null) {
            return "该班级已存在！";
        }
        LocalDateTime localDateTime = LocalDateTime.now().withNano(0);
        classes.setAddTime(localDateTime);
        if (classesRepository.save(classes) != null) {
            return "保存成功！";
        } else {
            return "保存失败！";
        }
    }

    @Override
    @CacheEvict(cacheNames = "classes", allEntries = true)
    public String updateClasses(Classes classes) {
        classesRepository.save(classes);
        return "修改成功！";
    }

    @Override
    @CacheEvict(cacheNames = "classes", allEntries = true)
    public String deleteClasses(Integer id) {
        classesRepository.deleteById(id);
        return "删除成功！";
    }

    @Override
    @Caching(evict = {@CacheEvict(cacheNames = "teacher", allEntries = true),
            @CacheEvict(cacheNames = "department", allEntries = true)})
    public String addDepartment(Department department) {
        if (departmentRepository.findByName(department.getName()) != null) {
            return "该系别已存在！";
        }
        LocalDateTime localDateTime = LocalDateTime.now().withNano(0);
        department.setAddTime(localDateTime);
        if (departmentRepository.save(department) != null) {
            Teacher teacher = teacherRepository.findAllByUsername(department.getTeachingDirectorUsername());  //
            String position = teacher.getPosition();
            String isdirectorteacher = teacher.getIsdirectorteacher();
            if (isdirectorteacher == null) {
                teacher.setIsdirectorteacher("1");
            } else {
                //所任系主任个数
                int i = Integer.parseInt(isdirectorteacher);
                i++;
                teacher.setIsdirectorteacher(String.valueOf(i));
            }
            if (position == null) {
                teacher.setPosition(department.getName() + "系教学主任");
            } else {
                teacher.setPosition(position + " " + department.getName() + "系教学主任");
            }
            teacherRepository.save(teacher);
            return "保存成功！";
        } else {
            return "保存失败！";
        }
    }

    @Override
    @CacheEvict(cacheNames = "department", allEntries = true)
    public String updateDepartment(Department department) {
        departmentRepository.save(department);
        return "修改成功！";
    }

    @Override
    @CacheEvict(cacheNames = "department", allEntries = true)
    public String deleteDepartment(Integer id) {
        Optional<Department> optionalDepartment = departmentRepository.findById(id);
        Department department = optionalDepartment.get();
        Teacher teacher = teacherRepository.findAllByUsername(department.getTeachingDirectorUsername());
        String isdirectorteacher = teacher.getIsdirectorteacher();
        int i = Integer.parseInt(isdirectorteacher);
        i--;
        teacher.setIsdirectorteacher(String.valueOf(i));
        teacherRepository.save(teacher);
        departmentRepository.deleteById(id);
        return "删除成功！";
    }

    @Override
    public College getCollegeById(Integer id) {
        return collegeRepository.findById(id).orElse(null);
    }

    @Override
    public Major getMajorById(Integer id) {
        return majorRepository.findById(id).orElse(null);
    }

    @Override
    public Classes getClassesById(Integer id) {
        return classesRepository.findById(id).orElse(null);
    }

    @Override
    public Department getDepartmentById(Integer id) {
        return departmentRepository.findById(id).orElse(null);
    }

    @Override
    @Cacheable(cacheNames = "department")
    public List<Department> getDepartmentByCollege(String college) {
        return departmentRepository.findAllByCollege(college);
    }

    @Override
    public String addLab(Lab lab) {
        if (labRepository.findByName(lab.getName()) != null) {
            return "该实验室已存在！";
        }
//        LocalDateTime localDateTime=LocalDateTime.now().withNano(0);
//        lab.setAddTime(localDateTime);
        if (labRepository.save(lab) != null) {
            return "保存成功！";
        } else {
            return "保存失败！";
        }
    }

    @Override
    public String updateLab(Lab lab) {
        labRepository.save(lab);
        return "修改成功！";
    }

    @Override
    public Lab getLabById(Integer id) {
        return labRepository.findById(id).orElse(null);
    }

    @Override
    public List<Lab> getAllLab() {
        return labRepository.findAll(Sort.by(Sort.Direction.DESC,"id"));
    }

    @Override
    public String deleteLab(Integer id) {
        labRepository.deleteById(id);
        return "删除成功！";
    }

    @Override
    @Cacheable(cacheNames = "college")
    public List<College> getAllCollege() {
        return collegeRepository.findAll(Sort.by(Sort.Direction.DESC,"id"));
    }

    @Override
    @Cacheable(cacheNames = "major")
    public List<Major> getMajorByCollege(String college) {
        return majorRepository.findAllByCollege(college);
    }

    @Override
    @Cacheable(cacheNames = "classes")
    public List<Classes> getClassesByMajor(String major) {
        return classesRepository.findAllByMajor(major);
    }
}
